#include"business.h"
#include<shadow.h>
#include <crypt.h>
#include "verification.h"


int clientbusiness(int sockfd,const char* ip,const char* port){
    train_t train;

    //命令的共享内容
    cmdShare_t *pCmdShare = (cmdShare_t*)malloc(sizeof(cmdShare_t));

    while(1){
        int ret=-1;
        char flag;
        puts("请选择你操作\n1.用户注册，2.用户登录,3.退出");
        while(1){
            flag = getchar();
            if(flag == '1'||flag == '2'){
                break;
            }else if(flag == '3'){
                return 0;
            }
        }
        getchar();
        if(flag == '1'){
            //生成请求
            char preId[256] = {0}; //用来保存id的前五位
            generateId("",preId);
            /* printf("preId = %s\n",preId); */
            cmdShareInit(pCmdShare,preId,"1","");
            char request[4096] ={0};
            sprintf(request,"%s %s %s",pCmdShare->id,pCmdShare->token,pCmdShare->command);
            /* printf("request = %s\n",request); */
            //发送请求
            memset(&train,0,sizeof(train_t));
            train.length = strlen(request);
            memcpy(train.data,request,train.length);
            send(sockfd,&train.length,sizeof(train.length),0);
            send(sockfd,train.data,train.length,0);
            ret = userRegister(sockfd,pCmdShare);
        }else if(flag == '2'){
            //生成请求
            char preId[256] = {0}; //用来保存id的前五位
            generateId("",preId);
            /* printf("preId = %s\n",preId); */
            memset(pCmdShare,0,sizeof(cmdShare_t));
            cmdShareInit(pCmdShare,preId,"2","");
            char request[4096] ={0};
            sprintf(request,"%s %s %s",pCmdShare->id,pCmdShare->token,pCmdShare->command);
            /* printf("request = %s\n",request); */
            //发送请求
            memset(&train,0,sizeof(train_t));
            train.length = strlen(request);
            memcpy(train.data,request,train.length);
            send(sockfd,&train.length,sizeof(train.length),0);
            send(sockfd,train.data,train.length,0);
            ret = pwdAuth(sockfd,pCmdShare);
        }
        /* printf("ret = %d\n",ret); */
        if(ret == 0){
            break;
        }else{
            printf("Failed!\n");
        }
    }


    //创建储存文件的目录
    mkdir("netDisk",0777);
    //perror("mkdir");
    char username[256] = {0};
    getusername(pCmdShare->id,username);
    /* printf("pCmdshare->id = %s,username = %s\n",pCmdShare->id,username); */
    while(1){
        //获取并显示当前工作目录
        //进行token验证,同时发送命令
        memset(pCmdShare->command,0,sizeof(pCmdShare->command));
        memcpy(pCmdShare->command,"reminder",strlen("reminder")+1);
        int vret = tokenVerify(sockfd,pCmdShare->id,pCmdShare->token,pCmdShare->command);
        if(vret != 0){
            //token验证不通过
            return -2;
        }
        char curWorkDir[1024] = {0};
        getReminder(sockfd,username,ip,port,curWorkDir);
        
        char command[2048] = {0};
        char ch;
        for(int i=0;i < 2048;i++)
        {
            ch = getchar();
            if(ch == '\n'){
                command[i] = '\0';
                break;
            }
            command[i] = ch;
        }
        /* printf("command = %s\n",command); */
        if(memcmp(command,"exit",strlen("exit"))==0){
            break;
        }
        memset(pCmdShare->command,0,sizeof(pCmdShare->command));
        memcpy(pCmdShare->command,command,strlen(command)+1);
        clientExecCommand(sockfd,pCmdShare,ip,port,username,curWorkDir);
    }
    free(pCmdShare);
    return 0;
}
